package cn.xsw;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


public class MyAutoGenerator {
    public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath  = System.getProperty("user.dir");//获取项目目录
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("liangYiHui");
        gc.setFileOverride(true);//是否覆盖
        gc.setOpen(false);//是否打开资源管理器
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setServiceName("s%Service");//去掉Service的I前缀
//        gc.setIdType(IdType.INPUT) //主键生成策略
        gc.setDateType(DateType.ONLY_DATE);//日期类型
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/studio2?userSSL=true&amp&useUnicode=true&amp&characterEncoding=UTF-8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("lyh123");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName("generator");//模块名
        pc.setParent("cn.xsw");
        pc.setEntity("entity");
        pc.setController("controller");
        pc.setMapper("mapper");
        pc.setService("service");
        mpg.setPackageInfo(pc);


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//下划线转驼峰
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);//自动lombok
        strategy.setRestControllerStyle(true);//rest风格
        strategy.setTablePrefix("tb_");
//        strategy.setInclude("tb_user","tb_carousel","tb_article","tb_article_category","tb_business","tb_business_category","tb_company");
        strategy.setInclude("tb_user");
//        strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段
        //自动填充配置
//        TableFill create_date = new TableFill("create_date", FieldFill.INSERT);
//        TableFill update_date = new TableFill("update_date", FieldFill.INSERT);
//        List<TableFill> list = new ArrayList<>();
//        list.add(create_date);
//        list.add(update_date);
//        strategy.setTableFillList(list);
        strategy.setVersionFieldName("version");//乐观锁
        strategy.setControllerMappingHyphenStyle(true);//controller地址驼峰
        mpg.setStrategy(strategy);

        //执行
        mpg.execute();

    }
}
